<template>
  <div class="query-list">
    <nanning-screen-search @on-input-search="onInputSearch" :placeholderValue="placeholderValue" @on-click-search="onClickSearch"></nanning-screen-search>
    <div class="query-list-content">
      <!-- <div class="query-list-content-title">共{{ totalCount }}条数据</div> -->
      <div class="loading-container" v-show="loadStatus">
        <screen-loading>加载中...</screen-loading>
      </div>
      <div class="swiper" v-show="queryList.length > 0 && !loadStatus">
        <div v-for="(item, index) in queryList" :key="index" class="swiper-slide">
          <div :class="['query-list-content-item', activeCode === item.id ? 'active' : '']" @click="onChangeItem(item.id)">
            <div class="name">{{ item.name }}</div>
            <div class="card">身份证：{{ item.idcard }}</div>
            <div class="unit">单位：{{ item.orgName }}</div>
            <div class="img" :style="{ background: 'url(' + item.imgUrl + ')' }">{{ item.empTypeName }}</div>
          </div>
        </div>
        <screen-load-more :loadMoreVisible="loadMoreVisible" :loadMoreType="loadMoreType"></screen-load-more>
      </div>
      <div class="nodata-container" v-show="queryList.length === 0 && !loadStatus">
        <div class="nodata-img">
          <img src="../../../../../assets/images/no-data.png" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { getSecurityList } from 'api/situationAwareness/securityPeople/securityPeople'
enum LoadMoreType {
  none = 'none',
  loading = 'loading'
}
@Component({
  name: 'query-list',
  components: {}
})
export default class QueryList extends Vue {
  private loadMoreVisible = false
  private loadMoreType: LoadMoreType = LoadMoreType.none
  private queryList: any = [] // 图片从1-5
  private activeCode = '1'
  private searchInputValue = ''
  private placeholderValue = '请输入安保人员'
  private pageNo = 1
  private pageSize = 10
  private timestamp = Date.now().toString()
  private stationName = ''
  private searchKey = ''
  private orgName = ''
  private totalCount = 0
  private loadStatus = true

  private onChangeItem(value: any) {
    this.activeCode = value
  }

  created() {
    this.getSecurityList()
  }

  mounted() {
    this.onReachBottom()
  }

  destroyed() {
    this.removeReachBottom()
  }
  // 获取搜索框内容
  private onInputSearch(value: any) {
    this.searchInputValue = value
  }

  private onClickSearch() {
    this.getSecurityList()
  }
  // 获取公交线路列表
  private async getSecurityList() {
    this.queryList = []
    this.loadMoreType = LoadMoreType.none
    this.loadMoreVisible = false
    this.timestamp = '2022-02-23 11:20:30'
    this.pageNo = 1
    this.loadStatus = true
    this.totalCount = 0
    const params = {
      pageIndex: this.pageNo,
      pageSize: this.pageSize,
      routeName: this.searchInputValue,
      dzcode: '',
      // timestamp: this.timestamp,
      timestamp: '2022-02-23 11:20:30',
      stationName: this.stationName,
      searchKey: this.searchKey,
      orgName: this.orgName
    }
    try {
      const res: any = await getSecurityList(params)
      this.totalCount = res.data.total || 0
      res.data.forEach((item: any) => {
        this.queryList.push({
          ...item,
          select: false,
          imgUrl: require('assets/images/situational-awareness/type1.png')
        })
      })
      this.loadMoreType = res.data.length === this.pageSize ? LoadMoreType.loading : LoadMoreType.none
      this.loadStatus = false
    } catch (err) {
      console.error('获取安保人员列表', err)
      this.loadMoreType = LoadMoreType.none
      this.queryList = []
      this.loadStatus = false
    }
  }
  // 下拉加载更多公交线路列表
  private async loadMoreSecurityList() {
    const params = {
      pageIndex: this.pageNo,
      pageSize: this.pageSize,
      routeName: this.searchInputValue,
      dzcode: '',
      // timestamp: this.timestamp,
      timestamp: '2022-02-23 11:20:30',
      stationName: this.stationName,
      searchKey: this.searchKey,
      orgName: this.orgName
    }
    try {
      const res: any = await getSecurityList(params)
      console.log('获取安保人员列表', res)
      res.data.forEach((item: any) => {
        this.queryList.push({
          ...item,
          select: false,
          imgUrl: require('assets/images/situational-awareness/type1.png')
        })
      })
      this.loadMoreType = res.data.length === this.pageSize ? LoadMoreType.loading : LoadMoreType.none
    } catch (err) {
      console.error('获取安保人员列表', err)
      this.loadMoreType = LoadMoreType.none
    }
  }

  onReachBottom() {
    // @ts-ignore
    document.querySelector('.swiper').addEventListener('scroll', this.reachBottomCb.bind(this))
  }
  // 移除触底检测
  private removeReachBottom() {
    // @ts-ignore
    document.querySelector('.swiper').removeEventListener('scroll', this.reachBottomCb.bind(this))
  }
  // 触底检测回调函数
  private reachBottomCb(e: any) {
    const { scrollHeight, scrollTop, clientHeight } = e.target
    if (clientHeight + scrollTop >= scrollHeight) {
      this.loadMoreVisible = true
      if (this.loadMoreType === LoadMoreType.loading) {
        this.pageNo += 1
        this.loadMoreSecurityList()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.query-list {
  box-sizing: border-box;
  width: 100%;
  height: calc(100% - 50px);
  padding: 30px 20px 24px 22px;
  &-content {
    box-sizing: border-box;
    width: 100%;
    height: calc(100% - 42px);
    padding-top: 30px;
    &-title {
      box-sizing: border-box;
      width: 100%;
      height: 25px;
      line-height: 25px;
      text-align: left;
      font-size: 14px;
      font-family: 'PingFang Regular';
      color: rgba(255, 255, 255, 0.6);
      padding-left: 15px;
    }
    .loading-container {
      width: 100%;
      height: calc(100% - 42px - 56px - 100px);
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .nodata-container {
      width: 100%;
      height: calc(100% - 42px - 56px - 100px);
      display: flex;
      align-items: center;
      justify-content: center;
      .nodata-img {
        width: 249px;
        height: 172px;
      }
    }
    .swiper {
      box-sizing: border-box;
      width: 100%;
      height: calc(100% - 25px);
      overflow-y: auto;
      &::-webkit-scrollbar {
        /*滚动条整体样式*/
        width: 6px; /*高宽分别对应横竖滚动条的尺寸*/
        height: 1px;
      }
      &::-webkit-scrollbar-thumb {
        /*滚动条里面小方块*/
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: #114da7;
      }
      &::-webkit-scrollbar-track {
        /*滚动条里面轨道*/
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        border-radius: 10px;
        background: #061836;
      }
      .swiper-slide {
        box-sizing: border-box;
        width: 100%;
        height: calc(100% / 7 - 7px);
        margin-bottom: 7px;
      }
      .query-list-content-item {
        position: relative;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        background: url('../../../../../assets/images/situational-awareness/shape-bac.png') center no-repeat;
        background-size: 100% 100%;
        font-family: 'PingFang Regular';
        padding-left: 45px;
        .name {
          display: flex;
          align-items: center;
          box-sizing: border-box;
          width: 100%;
          color: #fff;
          font-size: 18px;
          text-align: left;
        }
        .card {
          box-sizing: border-box;
          width: 100%;
          font-size: 16px;
          color: rgba(255, 255, 255, 0.67);
          text-align: left;
        }
        .unit {
          box-sizing: border-box;
          width: 100%;
          font-size: 16px;
          color: rgba(255, 255, 255, 0.67);
          text-align: left;
        }
        .img {
          background: url('../../../../../assets/images/situational-awareness/type1.png') center no-repeat;
          position: absolute;
          right: 1%;
          top: 0%;
          width: 122px;
          height: 28px;
          line-height: 28px;
          font-size: 16px;
          color: #fff;
          text-align: center;
        }
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          border-radius: 50%;
          background: #fff;
          display: block;
          position: absolute;
          left: 15px;
          top: 50%;
          transform: translate(0, -50%);
        }
      }
      .active {
        background: url('../../../../../assets/images/situational-awareness/shape-bac-active.png') center no-repeat;
        background-size: 100% 100%;
        .name {
          color: #00faf8;
          font-weight: bold;
        }
        .card {
          color: #00faf8;
        }
        .unit {
          color: #00faf8;
        }
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          border-radius: 50%;
          background: #00faf8;
          display: block;
          position: absolute;
          left: 15px;
          top: 50%;
          transform: translate(0, -50%);
        }
        .img {
          position: absolute;
          right: 2%;
          top: 5%;
        }
      }
    }
  }
}
</style>
